const express = require("express");
const path = require("path");
const authMiddleware = require("../middlewares/auth.middleware");
const wechatMiddleware = require("../middlewares/wechat.middleware");
const redis = require("redis");

const redisClient = redis.createClient();
const router = express.Router();

router.get("/", authMiddleware.getCode, (req,res,next)=>{
    //res.send("home");
    res.sendFile(path.join(__dirname, "../views/index.html"));
});
router.get("/userList", authMiddleware.getCode, (req,res,next)=>{
    res.send("userList");
});
router.get("/userDetail", authMiddleware.getCode, (req,res,next)=>{
    res.send("userDetil");
});
router.get("/movieList", authMiddleware.getCode, (req,res,next)=>{
    res.send("movieList");
});

router.get("/getUserInfo", authMiddleware.getAccess_token, authMiddleware.getUserInfo, (req,res,next)=>{
    console.log(req.query);
    let back_url = req.query.back_url;
    for(let item in req.query){
        if(item !== "back_url" && item !== "code" && item !== "state"){
            back_url += `&${item}=${req.query[item]}`;
        };
    };
    console.log(back_url);
    res.redirect(back_url);
});

router.get("/set", (req, res, next)=>{
    redisClient.set("username", "liuwenchao");
    res.send("hello world");
});

router.get("/get", (req, res, next)=>{
    redisClient.get("username", (err, result)=>{
        res.send(result);
    });
});

/*获取微信签名*/
router.get("/signature", wechatMiddleware.getAccess_token, wechatMiddleware.getTicket, wechatMiddleware.getSignature, function(req,res,next){

    res.send({signature: req.signature, nonceStr: req.nonceStr, timestamp: req.timestamp, appId: req.appId});
    //res.send({code: 0});
});

module.exports = router;